<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Description: 编辑一个地址
 *
 * @author      Eden
 * @datetime    2016/12/2 19:34
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */


class Edit_Model extends API_Model
{

    // 参数
    private $params = array();

    private $saveData = array();

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 主程序
     * @param array $params
     */
    public function run(Array $params)
    {
        $this->params = $params;
        $send['state'] = false;
        do {

            if (!$this->_checkParams()) {
                $send['error'] = $this->getError();
                break;
            }

            $addressId = $this->saveData['addressId'];
            unset($this->saveData['addressId']);

            try {

                $updateDb = clone($this->db);
                $updateDb->where('id', $addressId);
                if ($updateDb->update('user_address', $this->saveData)) {
                    $editDb = clone($this->db);
                    $query = $editDb->get_where('user_address', array('id' => $addressId));
                    $row = $query->row_array();
                    if (empty($row)) {
                        $send['error'] = '修改失败';
                        break;
                    }

                    if ($row['default'] == 1) {
                        $this->db->where('userId', $row['userId']);
                        $this->db->where_not_in('id', $row['id']);
                        $this->db->update('user_address', array('default' => 0));
                    }

                    $row['addressId'] = intval($row['id']);
                    unset($row['id']);
                    $row['userId'] = intval($row['userId']);
                    $row['default'] = !empty($row['default']);

                    $send['state'] = true;
                    $send['data'] = $row;
                } else {
                    $send['error'] = '修改失败';
                }

            } catch(PDOException $e) {
                $send['error'] = '修改失败';
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 检查参数
     */
    private function _checkParams()
    {
        $needs = array('addressId', 'name', 'phone', 'province', 'city', 'area', 'address', 'default');
        $data = array();

        if ($this->input->method(true) !== 'POST') {
            $this->setError('非法请求');
            return false;
        }

        foreach($needs as $need) {
            if (!array_key_exists($need, $this->params) || empty($this->params[$need])) {
                $this->setError('参数错误');
                return false;
            }
            $data[$need] = $this->params[$need];
        }

        if (!checkPhone($this->params['phone'])) {
            $this->setError('手机号格式错误');
            return false;
        }

        $this->saveData = $data;
        return true;
    }

}

